﻿using Sys.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sys.Core.Tour
{
    /// <summary>
    /// 线路景点业务
    /// </summary>
    public class RouteScenicBll : ExampleSort<TourRouteScenic, RouteScenicBll>
    {
        /// <summary>
        /// 排序实体
        /// </summary>
        public IQueryable<TourRouteScenic> SortEntities
        {
            get { return Entities.OrderBy(m => m.IntSort); }
        }

        /// <summary>
        /// 重写排序方法
        /// </summary>
        /// <param name="entities">查询数据对象</param>
        /// <param name="id">实体主键</param>
        /// <param name="sortname">排序字段</param>
        /// <param name="sortorder">排序方式</param>
        /// <param name="success">是否成功</param>
        /// <returns>结果信息</returns>
        public override string SetOrder(IQueryable<TourRouteScenic> entities, string id, string sortname, string sortorder, out bool success)
        {
            var model = GetByKey(id);

            entities = EntitiesOrder(model, entities, sortname, sortorder);

            var list = EntitiesPage(entities, 1, 1).ToList();

            success = false;

            if (list.Count > 0)
            {
                int temp = model.IntSort;
                model.IntSort = list[0].IntSort;
                list[0].IntSort = temp;

                list.Add(model);

                success = Update(list.Select(m => new TourRouteScenic()
                {
                    ID = m.ID,
                    IntSort = m.IntSort,
                    RouteID = m.RouteID,
                    ScenicID = m.ScenicID
                })) == 2;

                if (success)
                {
                    return "调整成功!";
                }
                else { return "调整失败!"; }
            }
            else { return "到头啦!"; }
        }
    }
}
